<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Users_mdl extends MY_Model {

    private static $list_user; //递归临时记录参数

    public function __construct() {
        parent::__construct();
        $this->table = 'users';
    }

    //获取所有上级
    public function get_from($uid){
        $this->db->where('id',$uid);
        $this->db->select('id,username,group_id,pid');
        $res = $this->get_one();
        self::$list_user[] = $res;

        if($res['pid'] > 0){
            return $this->get_from($res['pid']);
        }else{
            return self::$list_user;
        }
    }

    //上级所有数量+1
    //$pid 上级用户ID
    public function set_num($pid){
        $pid = intval($pid);
        $this->db->where('id',$pid);
        $this->db->select('id,pid');
        $res = $this->get_one();

        $this->db->where('id',$pid);
        $this->db->set('num','num+1',FALSE);
        $this->update();

        if($res['pid'] > 0 ){
            return $this->set_num($res['pid']);
        }else{
            return true;
        }
    }

    //获取充值金额， 判断是否升级
    public function set_group($uid){
        //Model
        $this->load->model('group_mdl');
        $this->load->model('bill_mdl');
        $this->load->model('mess_mdl');

        //获取用户当前级别
        $this->db->where('id',$uid);
        $this->db->select('username,group_id');
        $user_arr = $this->get_one();

        //查询当前级别的详细信息
        $this->db->where('id',$user_arr['group_id']);
        $group_res = $this->group_mdl->get_one();

        //查询比当前级别更高的信息
        $this->db->where('condition >',$group_res['condition']);
        $list = $this->group_mdl->select();
        if(empty($list)){
            //当没有比当前金额高的等级则退出
            return true;
        }else{
            //统计用户记录， 获取当前充值总金额
            $this->db->where('user_id',$uid);
            $this->db->where('charges >',0);
            $this->db->select_sum('charges');
            $money = $this->bill_mdl->select();
            $money = $money[0]['charges'];

            $tmp = array();
            foreach($list as $k => $v){
                if($v['condition'] < $money){
                    $tmp = $v;
                    unset($list[$k]);
                }
            }

            if(empty($tmp) || $tmp['id'] == $group_res['id']){
                return true;
            }
            //排序， 找到将要升级的层级
            $group_next = $tmp;
            foreach($list as $k => $v){
                if($group_next['condition'] < $v['condition'] && $v['condition'] != $group_next['condition']){
                    $group_next = $v;
                }
            }

            foreach($list as $k => $v){
                if($group_next['condition'] > $v['condition'] && $v['condition'] != $group_next['condition']){
                    $group_next = $v;
                }
            }

            //更改
            $this->db->where('id',$uid);
            $this->db->set('group_id',$tmp['id']);
            $this->update();
            //发送信息通知

            $mess = sprintf('尊敬的用户您好，恭喜您因为充值金额到达了 %s ,升级为 “<span style="color:red">%s</span>” 敬请知悉！',$tmp['condition'],$tmp['title']);
            $this->mess_mdl->send_message($mess,$uid);

            if(!empty($group_next)){
                $mess = sprintf('尊敬的用户您好，恭喜您升级为‘%s’,距离下一级 ‘%s’ 还需要充值积分‘%s’元，感谢您的支持',$tmp['title'],$group_next['title'],$group_next['condition']-$money);
                $this->mess_mdl->send_message($mess,$uid);
            }

            $amess = sprintf('管理员你好，用户‘%s’充值金额达到了 %s,升级为 “<span style="color:red">%s</span>” 敬请知悉！ <a href="%s">查看</a>',$user_arr['username'],$tmp['condition'],$tmp['title'],site_url('users/index?uid='.$uid));
            $this->mess_mdl->send_admin($amess);
            return true;
        }

    }

}

/* End of file Admini.php */
/* Location: ./application/models/Admini.php */